package com.ruoyi.api.web;

import com.itmuch.lightsecurity.jwt.JwtOperator;
import com.ruoyi.api.base.BaseApi;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.weixin.domain.Client;
import com.ruoyi.weixin.service.IAddressService;
import com.ruoyi.weixin.service.IClientService;
import com.ruoyi.weixin.service.IWxService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/api/client")
public class ClientApi extends BaseApi {
    @Autowired
    private JwtOperator operator;

    @Autowired
    private IWxService wxService;

    @Autowired
    private IAddressService addressService;

    @Autowired
    private IClientService clientService;

    /**
     * 绑定邮箱时获取验证码
     * @param email
     * @return
     */
    @PostMapping("/emailVerifi")
    public AjaxResult emailVerifi(String email){
        String regStr = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regStr);
        Matcher m = p.matcher(email);
        if (StringUtils.isEmpty(email) || (! m.matches())){
            return error("邮箱格式错误，获取失败");
        }
        Client c = new Client();
        c.setEmail(email);
        List<Client> clients = clientService.selectClientList(c);
        if (clients != null && clients.size() > 0){
            return error("这个邮箱已绑定其他账号");
        }
        if(clientService.emailVerifi(email)){
            return success("获取成功");
        }else {
            return error("未知错误，获取失败");
        }
    }

    @PostMapping("/emailVfied")
    public AjaxResult emailVfied(String email){
        String regStr = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regStr);
        Matcher m = p.matcher(email);
        if (StringUtils.isEmpty(email) || (! m.matches())){
            return error("邮箱格式错误，获取失败");
        }
        if(clientService.emailVerifi(email)){
            return success("获取成功");
        }else {
            return error("未知错误，获取失败");
        }
    }

    @PostMapping("/update")
    public AjaxResult update (Client client){
        Object o = clientService.updateClient(client);
        return success("修改成功", o);
    }



    @PostMapping("/emailBinding")
    public AjaxResult emailBinding(String clientId, String email, String verifi) throws Exception {
        String regStr = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
        Pattern p = Pattern.compile(regStr);
        Matcher m = p.matcher(email);
        if (StringUtils.isEmpty(email) || (! m.matches())){
            return error("绑定失败");
        }
        if (clientService.emailBinding(clientId, email, verifi)){
            return success("绑定成功", email);
        }else{
            return error("绑定失败");
        }
    }

    @PostMapping("/clientBirthday")
    public AjaxResult clientBirthday(Client client){
        if (clientService.clientBirthday(client)){
            return success("设置成功");
        }else{
            return error("已设置生日，不能再修改");
        }
    }


    /**
     * 修改登录密码
     * @param clientId
     * @param password
     * @param email
     * @param verifi
     * @return
     */
    @PostMapping("/loginPad")
    public AjaxResult loginPad(String clientId, String password, String email, String verifi){
        String regex = "^[\\w_-]{6,20}$";
        Pattern p = Pattern.compile(regex);
        Matcher m = p.matcher(password);
        if (m.matches()){
            if (clientService.loginPad(clientId, password, email, verifi)){
                return success("设置成功");
            }else {
                return error("设置失败");
            }
        }else return error("格式错误");
    }

    @PostMapping("/updateLoginPadVfi")
    public AjaxResult updateLoginPadVfi(String clientId, String email){
        Client client = new Client(clientId);
        client.setEmail(email);
        List<Client> clients = clientService.selectClientList(client);
        if (clients == null || clients.size() < 1){
            return error("邮箱错误");
        }else if (clientService.emailVerifi(email)){
            return success("获取成功");
        }else {
            return success("获取失败，未知错误");
        }
    }
}
